<?php

namespace App\Admin\Controllers;

use App\Models\Details;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SysDetailsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '账户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Details());

        $grid->column('id', __('ID'));
        $grid->column('order_sn', __('订单号'))->help('被邀请人成功下单之后的订单号');
        $grid->column('user.name', __('用户'));
        $grid->column('user.phone', __('用户手机号'));
        $grid->column('inviteeUser.name', __('被邀请人'))->help('数据属于邀请下单时才会有数值');
        $grid->column('inviteeUser.phone', __('被邀请人手机号'))->help('数据属于邀请下单时才会有数值');
        $grid->column('money', __('金额'));
        $grid->column('reward', __('返现百分比'))->display(function ($reward){
            if(empty($reward)){
                return '';
            }
            return $reward . '%';
        })->help('根据当前系统设置的返现百分比计算');
        $grid->column('status', __('状态'))->using([
            1 => '成功下单',
            2 => '订单未完成',
            3 => '返现已到账',
            4 => '提现'
        ], '未知')->dot([
            1 => 'success',
            2 => 'danger',
            3 => 'success',
            4 => 'warning',
        ], 'warning')->sortable();
        $grid->column('expect_at', __('预计到账时间'))->help('邀请下单时才会数值');
        $grid->column('created_at', __('创建时间'));

        $grid->expandFilter();

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {

                $filter->like('user.phone', __('用户手机号'));

                $filter->like('inviteeUser.phone', __('被邀请人手机号'));

                //$filter->between('created_at', __('注册时间'))->datetime();
            });

            $filter->column(1 / 2, function ($filter) {
                $filter->like('order_sn', __('订单号'));

                $filter->equal('status', __('订单状态'))->radio([
                    '' => '全部',
                    1 => '邀请下单',
                    2 => '订单未完成',
                    3 => '邀请成功',
                    4 => '提现',
                ]);

            });

        });
        //禁用创建按钮
        $grid->disableCreateButton();

        // 全部关闭
        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();

            // 去掉查看
            $actions->disableView();
        });
        // 去掉批量操作
        $grid->disableBatchActions();

        $grid->disableExport();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
//        $show = new Show(Details::findOrFail($id));
//
//        $show->field('id', __('Id'));
//        $show->field('order_sn', __('Order sn'));
//        $show->field('user_id', __('User id'));
//        $show->field('invitee_user_id', __('Invitee user id'));
//        $show->field('money', __('Money'));
//        $show->field('reward', __('Reward'));
//        $show->field('status', __('Status'));
//        $show->field('expect_at', __('Expect at'));
//        $show->field('created_at', __('Created at'));
//        $show->field('updated_at', __('Updated at'));
//
//        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        //$form = new Form(new Details());

//        $form->text('order_sn', __('Order sn'));
//        $form->number('user_id', __('User id'));
//        $form->number('invitee_user_id', __('Invitee user id'));
//        $form->decimal('money', __('Money'));
//        $form->number('reward', __('Reward'));
//        $form->switch('status', __('Status'));
//        $form->datetime('expect_at', __('Expect at'))->default(date('Y-m-d H:i:s'));

        //return $form;
    }
}
